package one.stand.controller;

import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.dto.SecurityLoginGzhApiRequest;
import com.jmp.base.dto.SecurityLoginGzhApiResponse;
import com.jmp.base.dto.SmsCaptchaRequest;
import com.jmp.base.dto.SmsCodeEnums;
import com.jmp.feign.SecurityFeign;
import com.jmp.feign.SmsFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import one.stand.advice.ResultEnum;
import one.stand.dto.LoginRequest;
import one.stand.dto.LoginResponse;
import one.stand.dto.LoginSmsRequest;
import one.stand.enums.LoginEnum;
import one.stand.model.BusinessCard;
import one.stand.model.Company;
import one.stand.model.ResultModel;
import one.stand.service.BusinessCardBaseService;
import one.stand.service.CompanyBaseService;
import one.stand.util.AssertUtil;
import one.stand.util.RequestContextHolder;
import one.stand.util.Result;
import one.stand.util.ResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author cjr
 */
@Api(description = "登录管理")
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private SecurityFeign securityFeign;
    @Autowired
    private SmsFeign smsFeign;
    @Autowired
    private BusinessCardBaseService businessCardBaseService;
    @Autowired
    private CompanyBaseService companyBaseService;

    @ApiOperation("登录")
    @PostMapping("")
    public Result<LoginResponse> login(@RequestBody LoginRequest request) {
        SecurityLoginGzhApiRequest wxRequest = SecurityLoginGzhApiRequest.builder()
                .code(request.getCode())
                .loginEnum(LoginEnum.GZH)
                .build();
        ResultModel<SecurityLoginGzhApiResponse> result = securityFeign.loginGzhApiWx(wxRequest);
        AssertUtil.isTrue(result.isSuccess(), result.getMessage(), ResultEnum.FAULT_LOGIN_WX.getCode());

        Integer inviteCompanyId = null;
        String inviteCompanyName = null;
        String inviteName = null;
        if (Objects.nonNull(request.getCardId())) {
            BusinessCard cardModel = businessCardBaseService.getCustomer(request.getCardId());
            inviteCompanyId = cardModel.getCompanyId();
            inviteName = cardModel.getName();
            Company companyModel = companyBaseService.getById(cardModel.getCompanyId());
            inviteCompanyName = companyModel.getCompanyName();
        }

        LoginResponse loginResponse = LoginResponse.builder()
                .accessToken(result.getData().getToken())
                .name(result.getData().getUserName())
                .icon(result.getData().getAvatarUrl())
                .register(StringUtils.isNoneBlank(result.getData().getPhone()))
                .hasCompany(Objects.nonNull(result.getData().getCardId()) && result.getData().getCardId() > 0)
                .inviteName(inviteName)
                .inviteCompanyName(inviteCompanyName)
                .inviteCompanyId(inviteCompanyId)
                .build();
        return ResultUtil.success(loginResponse, result.isSuccess());
    }

    @ApiOperation("发送验证码")
    @PostMapping("/sms")
    @SecurityAuthCheck
    public Result sms(@RequestBody LoginSmsRequest request) {
        String ip = RequestContextHolder.getRequestContext().getRequest().getIp();
        SmsCaptchaRequest captchaRequest = SmsCaptchaRequest.builder()
                .phone(request.getPhone())
                .type(SmsCodeEnums.login_sms.getCode())
                .ip(ip)
                .build();
        smsFeign.captchaSend(captchaRequest);
        return ResultUtil.success();
    }
}
